
package test.stat;

import java.util.ArrayList;
import java.util.List;

import dm.ast.DmASTNode;
import test.log.SqlLog;
import test.stat.Statictics;
import test.utility.SqlRun;

/* @ 2023-08-14
 * 错误信息统计
 * 统计执行时的错误信息
 */
public class StatError extends Statictics {
    //记录错误统计信息单元
    class ErrorTuple implements Comparable<ErrorTuple> {
        int errcode = 0;
        long count = 0;
        double rate = 0;
        String desc = "";
        String sql = null;

        public ErrorTuple(int errcode, String desc, String sql) {
            this.errcode = errcode;
            this.count = 1;
            this.desc = desc.replaceAll("\n", " ");
            this.sql = sql;
        }

        @Override
        public int compareTo(ErrorTuple o) { //需要根据count降序排列
            if (count > o.count) {
                return -1;
            } else if (count == o.count){
                return 0;
            }else{
                return 1;
            }
        }
    }

    public ArrayList<ErrorTuple> errorStats = new ArrayList<ErrorTuple>();

    public String getCount() {
        StringBuilder tmp = new StringBuilder();
        long cnt = 0;
        int i = 0;
        for (i = 0; i < errorStats.size(); i++) {
            cnt += errorStats.get(i).count;
        }
        tmp.append(String.format("%d\n", cnt));
        return tmp.toString();
    }

    @Override
    public void addSQLStat(String sql, SqlRun run) {
        int code = run.getErrCode();
        if (code == 0) {
            return;
        }
        addErrorTupleLow(code, run.getErrMsg(), sql, 1);
    }
        @Override
        public void combine (List < Statictics > stats) {
            errorStats.clear();
            for (Statictics stat : stats) {
                StatError statError = (StatError) stat;
                for (ErrorTuple sta :statError.errorStats){
                    addErrorTupleLow(sta.errcode, sta.desc, sta.sql, sta.count);
                }
            }
            errorStats.sort(null);
        }

        @Override
        public void output (SqlLog log){
            calcRate();
            StringBuilder tmp = new StringBuilder();
            for (int i = 0; i < errorStats.size(); i++) {
                ErrorTuple sta = errorStats.get(i);
                tmp.append(String.format("%.2f%%|%d|%d|%s|%s\n", sta.rate, sta.count, sta.errcode, sta.desc, sta.sql));
            }
            log.sqlLogMessage(tmp.toString());
            log.Flush();
        }
        private void addErrorTupleLow ( int errcode, String desc, String sql,long count){
            int i = 0;
            for (i = 0; i < errorStats.size(); i++) { //找到errorco相同的条目
                if (errcode == errorStats.get(i).errcode) {
                    break;
                }
            }
            if (i == errorStats.size()) { //没有找到errcode相同的项目,需要新增项目
                ErrorTuple sta = new ErrorTuple(errcode, desc, sql);
                sta.count = count;
                errorStats.add(sta);
            } else { //errcode相同的项目合并计数
                errorStats.get(i).count += count;
            }
        }
//计算每种错误的比例
        private void calcRate () {
            long cnt = 0;
            int i = 0;
            for (i = 0; i < errorStats.size(); i++) {
                cnt += errorStats.get(i).count;
            }
            for (i = 0; i < errorStats.size(); i++) {
                errorStats.get(i).rate = errorStats.get(i).count * 100.0 / cnt;
            }
        }
    }

